import { LoginParam, UserState, UserStore } from '@/types';
import { getStorageSync, setStorageSync } from '@/utils/storage';

const useUserStore: UserStore = {
  namespaced: true,
  state: () => ({
    token: getStorageSync('token') as string,
    name: 'miniprogram_template',
  }) as UserState,
  mutations: {
    SET_TOKEN: (state: UserState, token: string) => {
      token && setStorageSync('token', token);
      state.token = token;
    },
    SET_NAME: (state: UserState, name: string) => {
      state.name = name;
    },
  },
  actions: {
    login(store: any, loginParams: LoginParam) {
      store.commit('SET_NAME', loginParams);
    },
  },
};
export default useUserStore;
